package com.domain.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;

import com.domain.User;
import com.domain.service.UserService;
import com.domain.service.utils.ServiceUtils;
import com.views.LoginView;

public class LoginController {

	private LoginView loginView;
	private ActionListener actionListener;

	public LoginController(LoginView loginView) {

		this.loginView = loginView;
	}

	public void controll() {

		ActionListener actionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Object obj = e.getSource();

				if (obj.equals(loginView.getBtnSubmit())) {

					String userName = loginView.getTxtUserName().getText();
					String password = new String(loginView.getTxtPassword()
							.getPassword());

					if (userName.equals("") || password.equals("")) {

						loginView.getLblMessages().setText(
								"Fields with * are mandatory");
						loginView.getLblMessages().setVisible(true);

						if (userName.equals("")) {
							loginView.getLblUserNameError().setVisible(true);
							loginView.getTxtUserName().setBackground(
									Color.LIGHT_GRAY);
						} else {

							loginView.getLblUserNameError().setVisible(false);
							loginView.getTxtUserName().setBackground(
									Color.white);
						}
						if (password.equals("")) {
							loginView.getLblPasswordError().setVisible(true);
							loginView.getTxtPassword().setBackground(
									Color.LIGHT_GRAY);
						} else {
							loginView.getLblPasswordError().setVisible(false);
							loginView.getTxtPassword().setBackground(
									Color.white);
						}

					} else {

						UserService userService = ServiceUtils.getUserService();

						List<User> list = userService.getAll();

						Boolean success = false;
						for (int i = 0; i < list.size(); i++) {

							User user = list.get(i);

							if (user.getUserName().equals(userName)
									&& user.getPassword().equals(password)) {

								loginView
										.getLblMessages()
										.setText(
												"Congrats! You have successfully Logged in");
								loginView.getLblMessages().setVisible(true);
								success = true;
							}

						}
						if (success == false) {

							loginView.getLblMessages().setText(
									"Sorry! Please Check your details again");

							loginView.getLblMessages().setVisible(true);
							loginView.getLblUserNameError().setVisible(false);
							loginView.getLblPasswordError().setVisible(false);

							loginView.getTxtUserName().setBackground(
									Color.white);
							loginView.getTxtPassword().setBackground(
									Color.white);

						}

					}

				}
				if (obj.equals(loginView.getBtnReset())) {

					loginView.getTxtUserName().setText("");
					loginView.getTxtPassword().setText("");
					loginView.getTxtUserName().requestFocus();

					loginView.getLblMessages().setVisible(false);
					loginView.getLblUserNameError().setVisible(false);
					loginView.getLblPasswordError().setVisible(false);
					loginView.getTxtUserName().setBackground(Color.white);
					loginView.getTxtPassword().setBackground(Color.white);
				}

			}
		};

		loginView.getBtnSubmit().addActionListener(actionListener);
		loginView.getBtnReset().addActionListener(actionListener);

	}
}
